﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using Avalonia.Controls;
using Avalonia.Media;
using Avalonia.Threading;
using AvaloniaApplication1.IServices.NavigationInterface;
using AvaloniaApplication1.Service;
using AvaloniaApplication1.ViewModels.CardViewModel;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Messaging;
using SukiUI;
using SukiUI.Controls;
using SukiUI.Dialogs;
using SukiUI.Toasts;

namespace AvaloniaApplication1.ViewModels;

public partial class MainWindowViewModel : ObservableObject
{
    [ObservableProperty] private SukiSideMenuItem _selectedMenuItem;
    public ISukiDialogManager DialogManager { get; }
    [ObservableProperty] private bool _isDarkTheme = true;

    public ISukiToastManager ToastManager  { get; }
    /// <summary>
    /// 菜单项集合
    /// </summary>
    public ObservableCollection<SukiSideMenuItem> MenuItems { get; set; }


    /// <summary>
    /// 主题服务
    /// </summary>
    public ThemeService _ThemeService;

    public MainWindowViewModel(ThemeService themeService,
        SettingsPageViewModel settingsPageViewModel,
        HomePageViewModel homePageViewModel,
        DevicePageViewModel devicePageViewModel,
        ISukiDialogManager dialogManager,
        ISukiToastManager toastManager,
        TopicDisplayViewModel topicDisplayViewModel,
        ModbusCardViewModel.ModbusCardViewModel  modbusCardViewModel
    )
    {
        _ThemeService = themeService;
        DialogManager = dialogManager;
        ToastManager = toastManager;

        MenuItems = new ObservableCollection<SukiSideMenuItem>
        {
            new()
            {
                Header = "首页",
                Icon = new PathIcon
                {
                    Data = Geometry.Parse(
                        "M505.5488 23.9104l-447.5904 376.32a85.0432 85.0432 0 0 0 54.784 150.1696h76.8l3.7376 0.256c4.096 0.512 9.0112 6.0416 9.0112 12.544v326.144a122.2144 122.2144 0 0 0 122.0096 121.856h95.5904A89.6 89.6 0 0 0 509.4912 921.6v-179.2c0-31.3856 17.92-53.76 35.328-53.76h36.864c17.408 0 35.328 22.3744 35.328 53.76V921.6c0 49.4592 40.0896 89.6 89.6 89.6h95.5392a121.9584 121.9584 0 0 0 122.0608-121.856V563.2a12.8 12.8 0 0 1 12.8-12.8h76.8a85.0432 85.0432 0 0 0 54.6816-150.1696L620.8512 23.9104a89.6 89.6 0 0 0-115.3024 0z m65.8944 58.7776l447.6416 376.32a8.2432 8.2432 0 0 1-5.2736 14.592h-76.8A89.6 89.6 0 0 0 847.36 563.2v326.144c0 24.832-20.2752 45.056-45.2608 45.056H706.56a12.8 12.8 0 0 1-12.8-12.8v-179.2c0-70.5024-48.128-130.56-112.128-130.56h-36.864c-64 0-112.128 60.0576-112.128 130.56V921.6a12.8 12.8 0 0 1-12.8 12.8H324.352a45.3632 45.3632 0 0 1-45.2608-45.2608V563.2A89.6 89.6 0 0 0 199.936 474.112l-8.192-0.512L112.64 473.6a8.2432 8.2432 0 0 1-5.3248-14.592l447.5904-376.32a12.8 12.8 0 0 1 16.4864 0z"),
                    Width = 24,
                    Height = 24,
                },

                PageContent = homePageViewModel
            },
            new()
            {
                Header = "设置",
                Icon = new PathIcon
                {
                    Data = Geometry.Parse(
                        "M12,17.27L18.18,21L16.54,13.97L22,9.24L14.81,8.62L12,2.5L9.19,8.62L2,9.24L7.45,13.97L5.82,21L12,17.27Z")
                },
                PageContent = settingsPageViewModel
            },
            new()
            {
                Header = "设备列表",
                Icon = new PathIcon
                {
                    Data = Geometry.Parse("M12,20.5L23.19,12.5L12,5.5L0.81,12.5L12,20.5Z")
                },
                PageContent = devicePageViewModel
            },
            new()
            {
                Header = "Topic数据监听",
                Icon = new PathIcon
                {
                    Data = Geometry.Parse("M20 2H4c-1.1 0-2 .9-2 2v18l4-4h14c1.1 0 2-.9 2-2V4c0-1.1-.9-2-2-2z")
                },
                PageContent = topicDisplayViewModel
            },
            new()
            {
                Header = "Modbus设备管理",
                Icon = new PathIcon
                {
                    Data = Geometry.Parse("M20 2H4c-1.1 0-2 .9-2 2v18l4-4h14c1.1 0 2-.9 2-2V4c0-1.1-.9-2-2-2z")
                },
                PageContent = modbusCardViewModel
            }
        };

        SelectedMenuItem = MenuItems.FirstOrDefault();
    }
    
    partial void OnSelectedMenuItemChanged(SukiSideMenuItem value)
    {
        // 通知前一个页面即将离开
        if (SelectedMenuItem?.PageContent is IPageLifecycle previousPage)
        {
            previousPage.OnPageLeaving();
        }
        
        // 通知新页面即将进入
        if (value?.PageContent is IPageLifecycle newPage)
        {
            newPage.OnPageEntering();
        }
    }

    /// <summary>
    /// 主题切换
    /// </summary>
    public void ChangeTheme()
    {
        SukiTheme.GetInstance().SwitchBaseTheme();
        IsDarkTheme = !IsDarkTheme;

        #region 学习如何全局共享状态

        _ThemeService.IsDarkTheme = IsDarkTheme;

        #endregion

        // 切换主题后，发送主题变更消息
        WeakReferenceMessenger.Default.Send(new ThemeChangedMessage(IsDarkTheme));
    }
}